package com.jacobson.core.base

import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.jacobson.core.framework.annotation.IntentParameter
import com.jacobson.core.utils.ImmersiveExtensions
import com.jacobson.core.widget.logger.Logger
import com.jacobson.core.widget.logger.LoggerFactory
import com.trello.rxlifecycle2.android.ActivityEvent
import io.reactivex.subjects.BehaviorSubject

/**
 * ================================================
 *
 * @author Wucz on 2018/7/16 10:28
 * ================================================
 */
abstract class BaseActivity : AppCompatActivity(), IActivity {
    protected val logger: Logger = LoggerFactory.getLogger(this.javaClass.simpleName, this.javaClass.simpleName + " >>> ")
    public val mLifecycleSubject: BehaviorSubject<ActivityEvent> = BehaviorSubject.create<ActivityEvent>()

    override fun onCreate(savedInstanceState: Bundle?) {
        logger.i("BaseActivity-> preOnCreate: ${this::class.simpleName}}")
        super.onCreate(savedInstanceState)
        takeIf { keepScreenOn() }?.let {
            window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) //保持屏幕常亮
        }
        ImmersiveExtensions.immersive(this)
        try {
            val layoutResId = initView(savedInstanceState)
            if (layoutResId != 0) {
                setContentView(layoutResId)
                ImmersiveExtensions.fitsSystemWindows(this)
                ImmersiveExtensions.supportAndroidBug5497Workaround(this)
//                ImmersiveExtensions.fitNavigationBar(this)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        preInitData(savedInstanceState)
        initData(savedInstanceState)
        logger.i("BaseActivity-> postOnCreate: ${this::class.simpleName}}")
    }

    protected open fun keepScreenOn(): Boolean = false

    protected open fun preInitData(savedInstanceState: Bundle?) {
        logger.d("${this::class.simpleName}->PreInitData")
    }

    override fun onStart() {
        logger.i("BaseActivity-> preOnStart: ${this::class.simpleName}")
        super.onStart()
        logger.i("BaseActivity-> postOnStart: ${this::class.simpleName}")
    }

    override fun useEventBus(): Boolean {
        return true
    }

    override fun useFragment(): Boolean {
        return true
    }

    override fun injectBundleParameter() {
        logger.d("BaseActivity->injectBundleParameter: ${this::class.simpleName}")
        val extras = intent?.extras
        val injectActivity = this
        extras?.let {_ ->
            this.javaClass.declaredFields.forEach {
                it.getAnnotation(IntentParameter::class.java)?.run {
                    extras.get(key)?.run {
                        logger.d("setIntentParameter: ${it.name} = ${this}")
                        it.isAccessible = true
                        it.set(injectActivity, this)
                    } ?: run {
                        logger.w("require setIntentParameter: ${it.name} but not find value of key = $key")
                    }
                }
            }
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        doOnExit()
    }

    override fun finish() {
        super.finish()
        doOnExit()
    }

    protected open fun doOnExit() {

    }
}